<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class CrontabCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        return array(
            'status' => 'success',
            'msg'    => '',
            'data'   => $this->collection->transform(
                function ($item) {
                    $resultLabel = array(
                        0 => '未检测',
                        1 => '可用',
                        2 => '不可用',
                        3 => '解析失败'
                    );

                    return [
                        'id'                 => $item->id,
                        'user_host_id'       => $item->user_host_id,
                        'host'               => $item->host->host,
                        'port'               => $item->host->port,
                        'check_result'       => $item->check_result,
                        'check_result_label' => $resultLabel[$item->check_result],
                        'check_result_bgc'   => $item->check_result == 1? 'badge-success': ''
                    ];
                })
        );
    }
}
